{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Regiment"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Introduction:\n",
    "\n",
    "Special thanks to: http://chrisalbon.com/ for sharing the dataset and materials.\n",
    "\n",
    "### Step 1. Import the necessary libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T07:21:27.335787Z",
     "start_time": "2024-01-13T07:21:27.250351Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 2. Create the DataFrame with the following values:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-01-13T07:21:27.350720Z",
     "start_time": "2024-01-13T07:21:27.260466Z"
    }
   },
   "outputs": [],
   "source": [
    "raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks', 'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'], \n",
    "        'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd','1st', '1st', '2nd', '2nd'], \n",
    "        'name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze', 'Jacon', 'Ryaner', 'Sone', 'Sloan', 'Piger', 'Riani', 'Ali'], \n",
    "        'preTestScore': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3],\n",
    "        'postTestScore': [25, 94, 57, 62, 70, 25, 94, 57, 62, 70, 62, 70]}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 3. Assign it to a variable called regiment.\n",
    "#### Don't forget to name each column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T07:21:27.399336Z",
     "start_time": "2024-01-13T07:21:27.274201Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "      regiment company      name  preTestScore  postTestScore\n0   Nighthawks     1st    Miller             4             25\n1   Nighthawks     1st  Jacobson            24             94\n2   Nighthawks     2nd       Ali            31             57\n3   Nighthawks     2nd    Milner             2             62\n4     Dragoons     1st     Cooze             3             70\n5     Dragoons     1st     Jacon             4             25\n6     Dragoons     2nd    Ryaner            24             94\n7     Dragoons     2nd      Sone            31             57\n8       Scouts     1st     Sloan             2             62\n9       Scouts     1st     Piger             3             70\n10      Scouts     2nd     Riani             2             62\n11      Scouts     2nd       Ali             3             70",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>regiment</th>\n      <th>company</th>\n      <th>name</th>\n      <th>preTestScore</th>\n      <th>postTestScore</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>Nighthawks</td>\n      <td>1st</td>\n      <td>Miller</td>\n      <td>4</td>\n      <td>25</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>Nighthawks</td>\n      <td>1st</td>\n      <td>Jacobson</td>\n      <td>24</td>\n      <td>94</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>Nighthawks</td>\n      <td>2nd</td>\n      <td>Ali</td>\n      <td>31</td>\n      <td>57</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>Nighthawks</td>\n      <td>2nd</td>\n      <td>Milner</td>\n      <td>2</td>\n      <td>62</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>Dragoons</td>\n      <td>1st</td>\n      <td>Cooze</td>\n      <td>3</td>\n      <td>70</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>Dragoons</td>\n      <td>1st</td>\n      <td>Jacon</td>\n      <td>4</td>\n      <td>25</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>Dragoons</td>\n      <td>2nd</td>\n      <td>Ryaner</td>\n      <td>24</td>\n      <td>94</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>Dragoons</td>\n      <td>2nd</td>\n      <td>Sone</td>\n      <td>31</td>\n      <td>57</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>Scouts</td>\n      <td>1st</td>\n      <td>Sloan</td>\n      <td>2</td>\n      <td>62</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>Scouts</td>\n      <td>1st</td>\n      <td>Piger</td>\n      <td>3</td>\n      <td>70</td>\n    </tr>\n    <tr>\n      <th>10</th>\n      <td>Scouts</td>\n      <td>2nd</td>\n      <td>Riani</td>\n      <td>2</td>\n      <td>62</td>\n    </tr>\n    <tr>\n      <th>11</th>\n      <td>Scouts</td>\n      <td>2nd</td>\n      <td>Ali</td>\n      <td>3</td>\n      <td>70</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "regiment = pd.DataFrame(raw_data,columns = raw_data.keys())\n",
    "regiment.columns\n",
    "regiment"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 4. What is the mean preTestScore from the regiment Nighthawks?  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T07:21:27.410349Z",
     "start_time": "2024-01-13T07:21:27.284090Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "regiment\nNighthawks    15.25\nName: preTestScore, dtype: float64"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "regiment[regiment['regiment'] == 'Nighthawks'].groupby('regiment')['preTestScore'].mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 5. Present general statistics by company"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T07:34:09.523373Z",
     "start_time": "2024-01-13T07:34:09.510974Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "               preTestScore  postTestScore\ncompany                                   \n1st     count      6.000000       6.000000\n        mean       6.666667      57.666667\n        std        8.524475      27.485754\n        min        2.000000      25.000000\n        25%        3.000000      34.250000\n        50%        3.500000      66.000000\n        75%        4.000000      70.000000\n        max       24.000000      94.000000\n2nd     count      6.000000       6.000000\n        mean      15.500000      67.000000\n        std       14.652645      14.057027\n        min        2.000000      57.000000\n        25%        2.250000      58.250000\n        50%       13.500000      62.000000\n        75%       29.250000      68.000000\n        max       31.000000      94.000000",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>preTestScore</th>\n      <th>postTestScore</th>\n    </tr>\n    <tr>\n      <th>company</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"8\" valign=\"top\">1st</th>\n      <th>count</th>\n      <td>6.000000</td>\n      <td>6.000000</td>\n    </tr>\n    <tr>\n      <th>mean</th>\n      <td>6.666667</td>\n      <td>57.666667</td>\n    </tr>\n    <tr>\n      <th>std</th>\n      <td>8.524475</td>\n      <td>27.485754</td>\n    </tr>\n    <tr>\n      <th>min</th>\n      <td>2.000000</td>\n      <td>25.000000</td>\n    </tr>\n    <tr>\n      <th>25%</th>\n      <td>3.000000</td>\n      <td>34.250000</td>\n    </tr>\n    <tr>\n      <th>50%</th>\n      <td>3.500000</td>\n      <td>66.000000</td>\n    </tr>\n    <tr>\n      <th>75%</th>\n      <td>4.000000</td>\n      <td>70.000000</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>24.000000</td>\n      <td>94.000000</td>\n    </tr>\n    <tr>\n      <th rowspan=\"8\" valign=\"top\">2nd</th>\n      <th>count</th>\n      <td>6.000000</td>\n      <td>6.000000</td>\n    </tr>\n    <tr>\n      <th>mean</th>\n      <td>15.500000</td>\n      <td>67.000000</td>\n    </tr>\n    <tr>\n      <th>std</th>\n      <td>14.652645</td>\n      <td>14.057027</td>\n    </tr>\n    <tr>\n      <th>min</th>\n      <td>2.000000</td>\n      <td>57.000000</td>\n    </tr>\n    <tr>\n      <th>25%</th>\n      <td>2.250000</td>\n      <td>58.250000</td>\n    </tr>\n    <tr>\n      <th>50%</th>\n      <td>13.500000</td>\n      <td>62.000000</td>\n    </tr>\n    <tr>\n      <th>75%</th>\n      <td>29.250000</td>\n      <td>68.000000</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>31.000000</td>\n      <td>94.000000</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "regiment.groupby(\"company\").describe().stack()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 6. What is the mean of each company's preTestScore?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T07:35:18.117598Z",
     "start_time": "2024-01-13T07:35:18.097626Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "company\n1st     6.666667\n2nd    15.500000\nName: preTestScore, dtype: float64"
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "regiment.groupby(\"company\")['preTestScore'].mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 7. Present the mean preTestScores grouped by regiment and company"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T07:36:11.288033Z",
     "start_time": "2024-01-13T07:36:11.270406Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "regiment    company\nDragoons    1st         3.5\n            2nd        27.5\nNighthawks  1st        14.0\n            2nd        16.5\nScouts      1st         2.5\n            2nd         2.5\nName: preTestScore, dtype: float64"
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "regiment.groupby([\"regiment\",\"company\"])['preTestScore'].mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 8. Present the mean preTestScores grouped by regiment and company without heirarchical indexing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T07:37:44.728974Z",
     "start_time": "2024-01-13T07:37:44.719932Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "company      1st   2nd\nregiment              \nDragoons     3.5  27.5\nNighthawks  14.0  16.5\nScouts       2.5   2.5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>company</th>\n      <th>1st</th>\n      <th>2nd</th>\n    </tr>\n    <tr>\n      <th>regiment</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Dragoons</th>\n      <td>3.5</td>\n      <td>27.5</td>\n    </tr>\n    <tr>\n      <th>Nighthawks</th>\n      <td>14.0</td>\n      <td>16.5</td>\n    </tr>\n    <tr>\n      <th>Scouts</th>\n      <td>2.5</td>\n      <td>2.5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "regiment.groupby([\"regiment\",\"company\"])['preTestScore'].mean().unstack()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 9. Group the entire dataframe by regiment and company"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T07:44:08.619144Z",
     "start_time": "2024-01-13T07:44:08.579881Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "                          preTestScore  postTestScore\nregiment   company                                   \nDragoons   1st     count      2.000000       2.000000\n                   mean       3.500000      47.500000\n                   std        0.707107      31.819805\n                   min        3.000000      25.000000\n                   25%        3.250000      36.250000\n                   50%        3.500000      47.500000\n                   75%        3.750000      58.750000\n                   max        4.000000      70.000000\n           2nd     count      2.000000       2.000000\n                   mean      27.500000      75.500000\n                   std        4.949747      26.162951\n                   min       24.000000      57.000000\n                   25%       25.750000      66.250000\n                   50%       27.500000      75.500000\n                   75%       29.250000      84.750000\n                   max       31.000000      94.000000\nNighthawks 1st     count      2.000000       2.000000\n                   mean      14.000000      59.500000\n                   std       14.142136      48.790368\n                   min        4.000000      25.000000\n                   25%        9.000000      42.250000\n                   50%       14.000000      59.500000\n                   75%       19.000000      76.750000\n                   max       24.000000      94.000000\n           2nd     count      2.000000       2.000000\n                   mean      16.500000      59.500000\n                   std       20.506097       3.535534\n                   min        2.000000      57.000000\n                   25%        9.250000      58.250000\n                   50%       16.500000      59.500000\n                   75%       23.750000      60.750000\n                   max       31.000000      62.000000\nScouts     1st     count      2.000000       2.000000\n                   mean       2.500000      66.000000\n                   std        0.707107       5.656854\n                   min        2.000000      62.000000\n                   25%        2.250000      64.000000\n                   50%        2.500000      66.000000\n                   75%        2.750000      68.000000\n                   max        3.000000      70.000000\n           2nd     count      2.000000       2.000000\n                   mean       2.500000      66.000000\n                   std        0.707107       5.656854\n                   min        2.000000      62.000000\n                   25%        2.250000      64.000000\n                   50%        2.500000      66.000000\n                   75%        2.750000      68.000000\n                   max        3.000000      70.000000",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th></th>\n      <th>preTestScore</th>\n      <th>postTestScore</th>\n    </tr>\n    <tr>\n      <th>regiment</th>\n      <th>company</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"16\" valign=\"top\">Dragoons</th>\n      <th rowspan=\"8\" valign=\"top\">1st</th>\n      <th>count</th>\n      <td>2.000000</td>\n      <td>2.000000</td>\n    </tr>\n    <tr>\n      <th>mean</th>\n      <td>3.500000</td>\n      <td>47.500000</td>\n    </tr>\n    <tr>\n      <th>std</th>\n      <td>0.707107</td>\n      <td>31.819805</td>\n    </tr>\n    <tr>\n      <th>min</th>\n      <td>3.000000</td>\n      <td>25.000000</td>\n    </tr>\n    <tr>\n      <th>25%</th>\n      <td>3.250000</td>\n      <td>36.250000</td>\n    </tr>\n    <tr>\n      <th>50%</th>\n      <td>3.500000</td>\n      <td>47.500000</td>\n    </tr>\n    <tr>\n      <th>75%</th>\n      <td>3.750000</td>\n      <td>58.750000</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>4.000000</td>\n      <td>70.000000</td>\n    </tr>\n    <tr>\n      <th rowspan=\"8\" valign=\"top\">2nd</th>\n      <th>count</th>\n      <td>2.000000</td>\n      <td>2.000000</td>\n    </tr>\n    <tr>\n      <th>mean</th>\n      <td>27.500000</td>\n      <td>75.500000</td>\n    </tr>\n    <tr>\n      <th>std</th>\n      <td>4.949747</td>\n      <td>26.162951</td>\n    </tr>\n    <tr>\n      <th>min</th>\n      <td>24.000000</td>\n      <td>57.000000</td>\n    </tr>\n    <tr>\n      <th>25%</th>\n      <td>25.750000</td>\n      <td>66.250000</td>\n    </tr>\n    <tr>\n      <th>50%</th>\n      <td>27.500000</td>\n      <td>75.500000</td>\n    </tr>\n    <tr>\n      <th>75%</th>\n      <td>29.250000</td>\n      <td>84.750000</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>31.000000</td>\n      <td>94.000000</td>\n    </tr>\n    <tr>\n      <th rowspan=\"16\" valign=\"top\">Nighthawks</th>\n      <th rowspan=\"8\" valign=\"top\">1st</th>\n      <th>count</th>\n      <td>2.000000</td>\n      <td>2.000000</td>\n    </tr>\n    <tr>\n      <th>mean</th>\n      <td>14.000000</td>\n      <td>59.500000</td>\n    </tr>\n    <tr>\n      <th>std</th>\n      <td>14.142136</td>\n      <td>48.790368</td>\n    </tr>\n    <tr>\n      <th>min</th>\n      <td>4.000000</td>\n      <td>25.000000</td>\n    </tr>\n    <tr>\n      <th>25%</th>\n      <td>9.000000</td>\n      <td>42.250000</td>\n    </tr>\n    <tr>\n      <th>50%</th>\n      <td>14.000000</td>\n      <td>59.500000</td>\n    </tr>\n    <tr>\n      <th>75%</th>\n      <td>19.000000</td>\n      <td>76.750000</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>24.000000</td>\n      <td>94.000000</td>\n    </tr>\n    <tr>\n      <th rowspan=\"8\" valign=\"top\">2nd</th>\n      <th>count</th>\n      <td>2.000000</td>\n      <td>2.000000</td>\n    </tr>\n    <tr>\n      <th>mean</th>\n      <td>16.500000</td>\n      <td>59.500000</td>\n    </tr>\n    <tr>\n      <th>std</th>\n      <td>20.506097</td>\n      <td>3.535534</td>\n    </tr>\n    <tr>\n      <th>min</th>\n      <td>2.000000</td>\n      <td>57.000000</td>\n    </tr>\n    <tr>\n      <th>25%</th>\n      <td>9.250000</td>\n      <td>58.250000</td>\n    </tr>\n    <tr>\n      <th>50%</th>\n      <td>16.500000</td>\n      <td>59.500000</td>\n    </tr>\n    <tr>\n      <th>75%</th>\n      <td>23.750000</td>\n      <td>60.750000</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>31.000000</td>\n      <td>62.000000</td>\n    </tr>\n    <tr>\n      <th rowspan=\"16\" valign=\"top\">Scouts</th>\n      <th rowspan=\"8\" valign=\"top\">1st</th>\n      <th>count</th>\n      <td>2.000000</td>\n      <td>2.000000</td>\n    </tr>\n    <tr>\n      <th>mean</th>\n      <td>2.500000</td>\n      <td>66.000000</td>\n    </tr>\n    <tr>\n      <th>std</th>\n      <td>0.707107</td>\n      <td>5.656854</td>\n    </tr>\n    <tr>\n      <th>min</th>\n      <td>2.000000</td>\n      <td>62.000000</td>\n    </tr>\n    <tr>\n      <th>25%</th>\n      <td>2.250000</td>\n      <td>64.000000</td>\n    </tr>\n    <tr>\n      <th>50%</th>\n      <td>2.500000</td>\n      <td>66.000000</td>\n    </tr>\n    <tr>\n      <th>75%</th>\n      <td>2.750000</td>\n      <td>68.000000</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>3.000000</td>\n      <td>70.000000</td>\n    </tr>\n    <tr>\n      <th rowspan=\"8\" valign=\"top\">2nd</th>\n      <th>count</th>\n      <td>2.000000</td>\n      <td>2.000000</td>\n    </tr>\n    <tr>\n      <th>mean</th>\n      <td>2.500000</td>\n      <td>66.000000</td>\n    </tr>\n    <tr>\n      <th>std</th>\n      <td>0.707107</td>\n      <td>5.656854</td>\n    </tr>\n    <tr>\n      <th>min</th>\n      <td>2.000000</td>\n      <td>62.000000</td>\n    </tr>\n    <tr>\n      <th>25%</th>\n      <td>2.250000</td>\n      <td>64.000000</td>\n    </tr>\n    <tr>\n      <th>50%</th>\n      <td>2.500000</td>\n      <td>66.000000</td>\n    </tr>\n    <tr>\n      <th>75%</th>\n      <td>2.750000</td>\n      <td>68.000000</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>3.000000</td>\n      <td>70.000000</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "regiment.groupby(['regiment', 'company']).describe().stack()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 10. What is the number of observations in each regiment and company"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T07:44:53.211367Z",
     "start_time": "2024-01-13T07:44:53.194625Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "company  regiment  \n1st      Dragoons      2\n         Nighthawks    2\n         Scouts        2\n2nd      Dragoons      2\n         Nighthawks    2\n         Scouts        2\ndtype: int64"
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "regiment.groupby(['company', 'regiment']).size()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 11. Iterate over a group and print the name and the whole data from the regiment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-13T07:45:08.903332Z",
     "start_time": "2024-01-13T07:45:08.879432Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dragoons\n",
      "   regiment company    name  preTestScore  postTestScore\n",
      "4  Dragoons     1st   Cooze             3             70\n",
      "5  Dragoons     1st   Jacon             4             25\n",
      "6  Dragoons     2nd  Ryaner            24             94\n",
      "7  Dragoons     2nd    Sone            31             57\n",
      "Nighthawks\n",
      "     regiment company      name  preTestScore  postTestScore\n",
      "0  Nighthawks     1st    Miller             4             25\n",
      "1  Nighthawks     1st  Jacobson            24             94\n",
      "2  Nighthawks     2nd       Ali            31             57\n",
      "3  Nighthawks     2nd    Milner             2             62\n",
      "Scouts\n",
      "   regiment company   name  preTestScore  postTestScore\n",
      "8    Scouts     1st  Sloan             2             62\n",
      "9    Scouts     1st  Piger             3             70\n",
      "10   Scouts     2nd  Riani             2             62\n",
      "11   Scouts     2nd    Ali             3             70\n"
     ]
    }
   ],
   "source": [
    "# Group the dataframe by regiment, and for each regiment,\n",
    "for name, group in regiment.groupby('regiment'):\n",
    "    # print the name of the regiment\n",
    "    print(name)\n",
    "    # print the data of that regiment\n",
    "    print(group)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3 (ipykernel)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
